package client.ui;

import gwtupload.client.MultiUploader;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import phasebook.client.msgs.PhotoInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HeaderControl;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.tile.TileGrid;
import com.smartgwt.client.widgets.tile.events.RecordClickEvent;
import com.smartgwt.client.widgets.tile.events.RecordClickHandler;
import com.smartgwt.client.widgets.viewer.DetailViewerField;

public class ImagePicker implements ResponseHandler {
	private Window winModal = new Window();
	private TileGrid tileGrid = new TileGrid();
	
	public interface PickListener{
		void onImagePicked(Long imgId);
	}

	public ImagePicker(Controller controller) {
		HeaderControl close = new HeaderControl(HeaderControl.CLOSE,
			new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					winModal.hide();
					onPick(null);
				}
			}
		);
		winModal.setHeaderControls(HeaderControls.HEADER_LABEL, close);
		winModal.setWidth(700);
		winModal.setHeight(400);
		winModal.setTitle("Pick your picture");
		winModal.setShowMinimizeButton(false);
		winModal.setIsModal(true);
		winModal.setShowModalMask(true);
		winModal.centerInPage();

		DetailViewerField pictureField = new DetailViewerField("picture");
		pictureField.setType("image");
		pictureField.setImageURLPrefix("image?id=");
		pictureField.setImageWidth(186);
		pictureField.setImageHeight(120);

		DetailViewerField nameField = new DetailViewerField("date");

		tileGrid.setShowEdges(false);
		tileGrid.setTileWidth(194);
		tileGrid.setTileHeight(165);
		tileGrid.setHeight("80%");
		tileGrid.setWidth100();
		tileGrid.setCanReorderTiles(true);
		tileGrid.setShowAllRecords(true);
		tileGrid.setOverflow(Overflow.AUTO);
		tileGrid.setFields(pictureField, nameField);
		tileGrid.addRecordClickHandler(new RecordClickHandler() {
			public void onRecordClick(RecordClickEvent event) {
				winModal.hide();
				Long imgId = null;
				try{
					imgId = Long.parseLong(event.getRecord().getAttribute("picture"));
				}catch (Exception e) {}
				onPick(imgId);
			}
		});

		SectionStackSection gridSection = new SectionStackSection();
		gridSection.setExpanded(true);
		gridSection.setResizeable(true);
		gridSection.addItem(tileGrid);
		gridSection.setShowHeader(false);

		MultiUploader uploader = new MultiUploader();
		uploader.setHeight("100%");
		uploader.setTitle("Upload new Photos");
		Canvas uploaderCanvas = new Canvas();
		uploaderCanvas.setHeight("20%");
		uploaderCanvas.setShowEdges(false);
		uploaderCanvas.setOverflow(Overflow.AUTO);
		uploaderCanvas.addChild(uploader);

		SectionStackSection uploadsSection = new SectionStackSection("Uploads");
		uploadsSection.setExpanded(false);
		uploadsSection.setResizeable(true);
		uploadsSection.addItem(uploaderCanvas);

		SectionStack stack = new SectionStack();
		stack.setVisibilityMode(VisibilityMode.MULTIPLE);
		stack.addSection(gridSection);
		stack.addSection(uploadsSection);

		winModal.addItem(stack);
		
		controller.addResponseHandler(this);
	}

	public void show() {
		winModal.show();
	}

	@Override
	public void onResponse(CombinedResponse response) {
		List<PhotoInfo> photos = response.getPhotos();
		if (photos != null) {
			Record[] data = new Record[photos.size()];
			for (int i = 0; i < photos.size(); i++) {
				data[i] = new Record();
				data[i].setAttribute("picture", photos.get(i).getPhotoId());
				data[i].setAttribute("date", photos.get(i).getUploadTimestamp());
			}
			tileGrid.setData(data);
		}
	}
	
	private Set<PickListener> listeners = new HashSet<PickListener>();
	private void onPick(Long imgId){
		for(PickListener listener:listeners)
			listener.onImagePicked(imgId);
	}
	
	public void addPickListener(PickListener listener){
		listeners.add(listener);
	}

	public void removePickListener(PickListener listener) {
		listeners.remove(listener);
	}
}
